var SupplierVm = new Vue({
    el: '#addSupplierPage',

    data: {

        /* 供应商添加 */
        suppFormVisible: false,
        // /*下单按钮，防止重复下单*/
        // orderVisible: true,

        /* 远程查新供应商 */
        supplierLoading: false,
        supplierData:[],
        suppForm: {
            supplierName:'',
            supplierId:'',
            orderId: '',
            eoItemId: '',
            // originalItemId: '',
            cost: ''
        },
        suppOptions: [],
        /* 外协商选择页面 */
        supplierPageVisible: false,
        /* 外协商分页参数 */
        supplierPage: {
            currPage: 1, // 当前页
            pageSize: 8, // 分页数
            totalCount: 0, // 总条数
            tableData: [], //数据列
        },
        /* 外协商分页查询参数 */
        supplierQuery: {
            supplierName: '',
            contactName: ''
        },

        //添加校验规则;
        addRules: {
            supplierName: [{
                required: true, //是否必填
                message: '请填写供应商名称', //规则
                trigger: 'blur'  //何事件触发
            }],
            attributeName: [{
                required: true, //是否必填
                message: '请填写制作工序', //规则
                trigger: 'blur'  //何事件触发
            }],
            cost: [{
                required: true, //是否必填
                message: '请输入正确的数值',
                trigger: 'blur'
            }]
        }
    },

    methods: {

        loadData: function(row){
            SupplierVm.suppFormShow(row)
        },
        /* 外协商添加工艺表单 */
        suppFormShow: function (row) {
            SupplierVm.suppForm.orderId = row.orderNo;
            SupplierVm.suppForm.eoItemId = row.eoItemId;
            SupplierVm.suppForm.originalItemId = row.originalItemId;
            SupplierVm.querySupplierPageList();
            SupplierVm.supplierPageVisible = true;
        },

        /* 查询外协商列表 */
        querySupplierPageList: function () {
            var queryData = {
                "limit": SupplierVm.supplierPage.pageSize,
                "page": SupplierVm.supplierPage.currPage,
                "supplierName": SupplierVm.supplierQuery.supplierName
            }
            $.ajax({
                type: "GET",
                url: baseURL + "suppOffline/erpsupplieroffline/list",
                contentType: "application/json",
                data: queryData,
                success: function (r) {
                    SupplierVm.supplierPage.tableData = r.page.list,
                    SupplierVm.supplierPage.totalCount = r.page.totalCount
                }
            });
        },

        /* 远程查询供应商; */
        serachSupplier: function(query){
            var self=this;
            debugger
            if (query !== ''){
                self.supplierLoading = true;
                var url = "suppOffline/erpsupplieroffline/list";
                var param = {'supplierName':query};
                setTimeout(function () {
                    $.ajax({
                        type: "GET",
                        url: baseURL + url,
                        contentType: "application/json;charset=UTF-8",
                        data:param,
                        success: function (r) {
                            if (r.code === 0){
                                self.supplierData = r.page.list;
                                self.supplierLoading = false;
                            } else {
                                alert(r.msg);
                            }
                        }
                    })
                },400)
            }else {
                self.supplierData = [];
            }
        },

        selectSupplier: function(val){
            for (var i=0;i<this.supplierData.length;i++){
                var item = this.supplierData[i];
                if(item.supplierId == val){
                    this.suppForm.supplierId=item.supplierId;
                    this.suppForm.supplierName=item.supplierName;
                }
            }
        },

        /*分页数改变*/
        handleSizeChange: function (val) {
            SupplierVm.supplierPage.pageSize = val;
            SupplierVm.querySupplierPageList();
        },
        /*分页当前页改变改变*/
        handleCurrentChange: function (val) {
            SupplierVm.supplierPage.currPage = val
            SupplierVm.querySupplierPageList();
        },
        /* 选择外协商 */
        suppFormCheck: function (row) {
            SupplierVm.suppForm.supplierId = row.supplierId;
            SupplierVm.suppForm.attributeName = '';
            SupplierVm.suppForm.cost = 0;
            SupplierVm.suppForm.csRemark = '';
            SupplierVm.suppFormVisible = true;
        },

        /* 保存外协商信息 */
        suppFormSave: function () {
            var postData = {
                orderId: SupplierVm.suppForm.orderId,
                eoItemId: SupplierVm.suppForm.eoItemId,
                supplierId: SupplierVm.suppForm.supplierId,
                supplierName: SupplierVm.suppForm.supplierName,
                cost: SupplierVm.suppForm.cost,
                attributeName: SupplierVm.suppForm.attributeName,
                csRemark: SupplierVm.suppForm.csRemark
            };
            this.$refs['addSupplierForm'].validate(function (valid) {
                if(valid){
                    $.ajax({
                        type: "POST",
                        url: baseURL + "twbOrder/erporder/twbsuppSave",
                        contentType: "application/json",
                        data: JSON.stringify(postData),
                        success: function (r) {
                            if (r.code == 0) {
                                SupplierVm.suppFormVisible = false;
                                SupplierVm.supplierPageVisible = false;
                                twbOrderDetailVm.getInfo(SupplierVm.suppForm.orderId);
                                SupplierVm.suppForm=
                                    {
                                        supplierName:'',
                                        supplierId:'',
                                        orderId: '',
                                        eoItemId: '',
                                        // originalItemId: '',
                                        cost: ''
                                    }
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                }
            })
        },
    },
});
